# Copyright (c) 2020 cgsdfc
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
"""
Generate cc source code using data and template.
"""

import pickle
from argparse import ArgumentParser
from pathlib import Path

from astlib.mako_utils import CreateMakoTemplate
from astlib.format_utils import FormatCodeInplace


def GenerateSourceFile(template, data_file, output):
    with open(data_file, 'rb') as f:
        data = pickle.load(f)
    template = CreateMakoTemplate(template)
    render_result = template.render(**data)

    Path(output).parent.mkdir(parents=True, exist_ok=True)

    with open(output, 'w') as f:
        f.write(render_result)
    FormatCodeInplace(output)


if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument('-t', '--template')
    parser.add_argument('-o', '--output')
    parser.add_argument('--data', help='data.pkl')
    args = parser.parse_args()

    GenerateSourceFile(template=args.template,
                       data_file=str(args.data),
                       output=args.output)
